package cn.edu.hcnu.servlet;

import cn.edu.hcnu.dao.IEmpDao;
import cn.edu.hcnu.dao.impl.EmpDaoImpl;
import cn.edu.hcnu.model.EmpDao;
import cn.edu.hcnu.unitl.PageModel;
//import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class EmpServlet extends HttpServlet {

    IEmpDao empDao=new EmpDaoImpl();//赋值左边为父类，右边为子类

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String uri=request.getRequestURI();
        System.out.println(uri);
        uri=uri.substring(1);

        if("viewEmp".equals(uri)){
            viewEmp(request,response);

        }else  if("addEmp".equals(uri)){
            addEmp(request,response);

        }else  if("deleteEmp".equals(uri)){
            deleteEmp(request,response);
        }else  if("updateEmp".equals(uri)){
            updateEmp(request,response);
        }else  if("modifyEmp".equals(uri)){
            modifyEmp(request,response);
        }
    }

    public void addEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String empno=request.getParameter("empno");
        String empname=request.getParameter("empname");
        String hiredate=request.getParameter("hiredate");

        PageModel pageModel=empDao.addEmp(empno,empname,hiredate);

        request.getRequestDispatcher("viewEmp").forward(request,response);

    }
    public void viewEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page=request.getParameter("page");
        if(page==null){
            PageModel pm=empDao.viewEmp(page);
            request.setAttribute("pm",pm);//查询后的员工信息
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }else{
            PageModel pm=empDao.viewEmp(page);
            Gson gson=new Gson();
            //System.out.println("Gson数据"+gson.toJson(pm));
            //服务器给客户端（浏览器)响应数据（把json格式的数据传送给浏览器)
            response.setContentType("application/json; charset=UTF-8");//设置给客户
            PrintWriter out=response.getWriter();
            out.write(gson.toJson(pm));//把json数据发送给浏览器

            out.flush();//刷新缓冲区，把缓冲区数据发送走，清空缓冲区
            out.close();
            //response.sendRedirect("index.jsp");
        }


    }
    public void deleteEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String empno=request.getParameter("empno");
        PageModel pageModel=empDao.deleteEmp(empno);
        response.sendRedirect("viewEmp");
//            request.getRequestDispatcher("emp").forward(request,response);
//            request.getRequestDispatcher("index.jsp").forward(request,response);
    }
    public void updateEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String empno=request.getParameter("empno");
        PageModel pm=empDao.updateEmpaddEmp(empno);
        request.setAttribute("emp1",pm);

        request.getRequestDispatcher("user_list_modify.jsp").forward(request,response);

    }
    public void modifyEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String empno=request.getParameter("empno");
        String empname=request.getParameter("empname");
        String hiredate=request.getParameter("hiredate");
        PageModel pageModel=empDao.modifyEmpaddEmp(empno,empname,hiredate);
        response.sendRedirect("viewEmp");
    }

}
